package com.shanzmoo.db.stm.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.shanzmoo.base.bean.PageRespBase;
import com.shanzmoo.base.bean.SResponse;
import com.shanzmoo.db.stm.entity.StmSysRoleEntity;
import com.shanzmoo.db.stm.entity.vo.StmSysRoleVO;
import com.shanzmoo.db.stm.entity.query.StmSysRoleQuery;
import com.shanzmoo.db.stm.mapper.StmSysRoleMapper;
import com.shanzmoo.db.stm.service.IStmSysRoleMenuService;
import com.shanzmoo.db.stm.service.IStmSysRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 仓储用户系统角色 服务实现类
 *
 * @author Gzhao 2021-03-10
 */
@Service
public class StmSysRoleServiceImpl extends ServiceImpl<StmSysRoleMapper, StmSysRoleEntity>
        implements IStmSysRoleService {

    @Autowired
    private IStmSysRoleMenuService stmSysRoleMenuService;

    @Override
    public PageRespBase<StmSysRoleVO> queryPage(StmSysRoleQuery query) {
        Page page = PageHelper.startPage(query.getPageNum(), query.getPageSize());
        List<StmSysRoleVO> reList = baseMapper.queryPage(query);

        PageRespBase<StmSysRoleVO> result = PageRespBase.create(page, reList);
        PageHelper.clearPage();
        return result;
    }

    @Override
    public SResponse<Boolean> add(StmSysRoleEntity entity, List<Integer> menuIds) {
        baseMapper.insert(entity);

        stmSysRoleMenuService.saveRoleMenuIds(entity.getRoleId(), menuIds);
        return SResponse.ok();
    }
}
